﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class LegalClientForm : Form
    {
        bool flag = false;       
        
        public LegalClientForm()
        {
            InitializeComponent();
        }

        public void refreshTable()
        {
            DBWorkClass dbWork = new DBWorkClass();
            LegalClientTypeClass selectMass = new LegalClientTypeClass();
            selectMass = dbWork.SelectFromLegalClient();
            int j = 0;
            if (selectMass.idClient.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
                dataGridView1[3, 0].Value = "";

            }
            else { dataGridView1.RowCount = selectMass.idClient.Count; }
            while (j < selectMass.idClient.Count)
            {
                dataGridView1[0, j].Value = selectMass.Title[j].ToString();
                dataGridView1[1, j].Value = selectMass.INN[j].ToString();
                dataGridView1[2, j].Value = selectMass.Capital[j].ToString();
                dataGridView1[3, j].Value = selectMass.Type[j].ToString();
                dataGridView1[4, j].Value = selectMass.Founders[j].ToString();
                j++;
            }
           
        }

        private int GetIdClient()
        {
            DBWorkClass dbWork = new DBWorkClass();
            LegalClientTypeClass selectMass = new LegalClientTypeClass();
            selectMass = dbWork.SelectFromLegalClient();
            int index = dataGridView1.CurrentRow.Index;
            int id_client = Convert.ToInt32(selectMass.idClient[index]);
            return id_client;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DBWorkClass dbWork = new DBWorkClass();
                dbWork.DeleteFromLegalClient(this.GetIdClient());
                this.refreshTable();
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
            

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnAccount.Enabled = true;
        }

        private void btnAccount_Click(object sender, EventArgs e)
        {
            try
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 1:
                        {
                            AccumulationForm accumForm = new AccumulationForm(this.GetIdClient(), "юридический");
                            accumForm.Show();
                            break;
                        }
                    case 0:
                        {
                            LoanForm loanForm = new LoanForm(this.GetIdClient(), "юридический");
                            loanForm.Show();
                            break;
                        }
                    case 2:
                        PaymentForm payForm = new PaymentForm(this.GetIdClient());
                        payForm.Show();
                        break;
                }
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddLegalClientForm addLegalClient = new AddLegalClientForm(this);
            addLegalClient.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LegalClientForm_Load(object sender, EventArgs e)
        {
            this.refreshTable();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                DBWorkClass dbWork = new DBWorkClass();
                LegalClientTypeClass selectMass = new LegalClientTypeClass();
                selectMass = dbWork.SelectFromLegalClient();
                string title = selectMass.Title[index].ToString();
                string inn = selectMass.INN[index].ToString();
                string founders = selectMass.Founders[index].ToString();
                string type = selectMass.Type[index].ToString();
                int capital = Convert.ToInt32(selectMass.Capital[index]);
                int id = Convert.ToInt32(selectMass.idClient[index]);
                EditLegalClientForm editForm = new EditLegalClientForm(title, inn, founders, capital, type, id, this);
                editForm.Show();
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }
    }
}
